package com.sap.apitest2.deprecated.client;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import com.sap.apitest2.deprecated.service.IService;


public class CatalogTest extends AbstractTest implements IService{

	
    public static void main(String args[]) throws Exception {
        

    	//testRequestJsonGetCatalogModel();
    	testRequestJsonGetCatalogs();
    	//testRequestJsonPutCatalog();
    	//testRequestJsonRemoveCatalog();
    	//testRequestJsonGetCatalog();
    	
        System.out.println("\n"); 
        System.exit(0);
    }
    
	@SuppressWarnings("unused")
	private static void testRequestJsonGetCatalogModel()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get single catalog model, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_CATALOG+"/1");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
	@SuppressWarnings("unused")
	private static void testRequestJsonGetCatalog()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to get single catalog info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_CATALOG+"/1");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();
       
        try {
            int result = httpclient.executeMethod(get); 
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();             
        }
    }
    private static void testRequestJsonGetCatalogs()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP GET request to query catalogs info, expect JSON");
        GetMethod get = new GetMethod(getTestHost()+ACTION_CATALOGS+"/100");
        get.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(get);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(get.getResponseBodyAsString());
        } finally {
            get.releaseConnection();
            
        }
    }   
    private static void testRequestJsonPutCatalog()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP POST request to save catalog info, use JSON, expect RESPONSE");
        PutMethod post = new PutMethod(getTestHost()+ACTION_SAVECATALOG+"/0");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
        	
        	//post.setRequestEntity(new StringRequestEntity(" {\"Catalog\":[{\"id\":0,\"name\":\"CatalogTestX\",\"type\":1}]}"));
        	post.setRequestEntity(new StringRequestEntity(" {\"Catalog\":[{\"id\":0,\"name\":\"CatalogTestX\"}]}"));

            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }
    @SuppressWarnings("unused")
    private static void testRequestJsonRemoveCatalog()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP POST request to remove catalog, use JSON, expect RESPONSE");
        PostMethod post = new PostMethod(getTestHost()+ACTION_REMOVECATALOG+"/102");
        post.addRequestHeader("Accept" , "application/json");
        HttpClient httpclient = new HttpClient();

        try {
        	
            int result = httpclient.executeMethod(post);
            
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            System.out.println(post.getResponseBodyAsString());
        } finally {
            post.releaseConnection();            
        }
    }
 /*   private static void testRequestJsonPutCatalog2()throws Exception{
    	System.out.println("\n");
        System.out.println("Sent HTTP POST request to query catalog info, use JSON, expect RESPONSE");
        //PostMethod post = new PostMethod("http://localhost:8080/BpcTesterWeb/savecatalog");
        HttpPost post = new HttpPost("http://localhost:8080/BpcTesterWeb/bpcsavecatalog");
        //post.addRequestHeader("Accept" , "application/json");
        //post.addRequestHeader(headerName, headerValue)
        DefaultHttpClient  httpclient = new DefaultHttpClient ();

        try {
//            JSONObject json = new JSONObject("");

//            Catalog catalog=new Catalog();
//            catalog.setName("test1");
//            catalog.setType(1);
//
//            json.put("Catalog", catalog);

//            System.out.println("body: "+json.toString());

           StringEntity se = new StringEntity(" {\"Catalog\":[{\"id\":0,\"name\":\"DEMO1\",\"type\":1}]}");

            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

            post.setEntity(se);
        	
            //post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

            HttpResponse httpResponse = httpclient.execute(post);

            int httpCode = httpResponse.getStatusLine().getStatusCode();




            System.out.println("Response status code: " + httpCode);
            //System.out.println("Response body: ");
            //System.out.println(post.getResponseBodyAsString());
        }  finally {

            if (httpclient != null) {

                httpclient.getConnectionManager().shutdown();// æœ€å�Žå…³æŽ‰é“¾æŽ¥ã€‚

                httpclient = null;

            }

        }


    }*/
}
